import { _decorator, BlockInputEvents, CCBoolean, Component, Node, Size, UITransform, view } from 'cc';
import { UIBase } from './UIBase';
const { ccclass, property } = _decorator;

//再当前UIBase的基础上，额外处理弹窗一些逻辑。
@ccclass('UIWindow')
export class UIWindow extends UIBase {
    protected _blockNode:Node = null;
    private _uiTransform:UITransform = null;
    get uiTrasform(){
        if(!this._uiTransform){
            this._uiTransform = this.node.getComponent(UITransform);
        }
        return this._uiTransform;
    }
   
    private _blockSize:Size = null;

    @property({displayName:'是否全屏拦截',tooltip:'勾起来全屏，不勾以当前UI节点大小进行拦截'})
    isBlockFullScreen:boolean = false;
   
    //这里要重写继承下来的init
    init(...rest: any[]): void {
        //处理触摸穿透问题
        this._createBlockNode();
        //再调用uibase的init
        super.init.apply(this,arguments);
    }

    private _createBlockNode(){
        //默认当前弹窗的大小。
        this._blockSize = this.uiTrasform.contentSize;
        this._blockNode = new Node();
        this._blockNode.parent =this.node;
        this._blockNode.setSiblingIndex(-100);
        //添加拦截组件
        this._blockNode.addComponent(BlockInputEvents);

        //给这个添加UITransfrom组件
        let blockUITransform = this._blockNode.addComponent(UITransform);
        //设置当前这个拦截区域节点的大小
        blockUITransform.contentSize = this._blockSize;

        //如果是希望全屏拦截，那么需要调整拦截区域大小为屏幕大小
        if(this.isBlockFullScreen){
            blockUITransform.contentSize = view.getDesignResolutionSize();
            //拦截节点的偏移，确保他一定要居中。
            let x = this.node.position.x;
            let y = this.node.position.y;
            this._blockNode.setPosition(-x,-y);
        }
    }
}


